﻿using System;
using Cuce.ReportingServices.SSRSWS;

namespace Cuce.ReportingServices
{
    /// <summary>
    /// Base Wrapper for Testing Reporting Service Reports
    /// </summary>
    public class TestWrapper
    {

        /// <summary>
        /// Initializes a new instance of the <see cref="TestWrapper"/> class.
        /// </summary>
        /// <param name="value">The value.</param>
        public TestWrapper(IReportingService value)
        {
            mService = value;
        }

        /// <summary>
        /// The Url of the Report Server
        /// </summary>
        public string ReportServer { get; set; }

        /// <summary>
        /// This function determines if the system is alive or not
        /// </summary>
        /// <returns></returns>
        public bool IsAlive()
        {
            //TODO : Test that service is alive
            return true;
        }

        private IReportingService mService;

        private void Setup()
        {
            mService.Url = ReportServer + "/ReportService.asmx";
            //  mService.Credentials = System.Net.CredentialCache.DefaultCredentials;
        }

        /// <summary>
        /// Proxy for Web Service
        /// </summary>
        public System.Net.IWebProxy WebProxy
        {
            set
            {
                mService.Proxy = value;
            }
            get
            {
                return mService.Proxy;
            }
        }


        /// <summary>
        /// Login Credentials for Web Service
        /// </summary>
        public System.Net.ICredentials Credentials
        {
            set
            {
                mService.Credentials = value;
            }
            get
            {
                return mService.Credentials;
            }
        }


        /// <summary>
        /// Render Report
        /// </summary>
        /// <param name="reportName">Name of Report</param>
        /// <param name="parameterList">Parameter List</param>
        /// <param name="outputType">Output Type</param>
        /// <returns>Rendered File</returns>
        public byte[] RenderReport(ReportName reportName, ParameterValue[] parameterList, OutputType outputType)
        {

            if (string.IsNullOrEmpty(ReportServer))
                throw new ArgumentException("ReportServer is null", "ReportServer");

            if (reportName == null)
                throw new ArgumentException("Report Name is null", "reportName");

            if (parameterList == null)
                throw new ArgumentException("parameterList is null", "parameterList");

            Setup();

            IsAlive();


            string devInfo = @"<DeviceInfo><Toolbar>False</Toolbar></DeviceInfo>";
            string encoding;
            string mimeType;
            Warning[] warnings = null;
            ParameterValue[] reportHistoryParameters = null;
            string[] streamIDs = null;
            ParameterValue[] reportParameters = parameterList;


            byte[] result = null;

            try
            {
                result = mService.Render(reportName.ToString(), outputType.ToString(), null, devInfo, reportParameters, null,
              null, out encoding, out mimeType, out reportHistoryParameters, out warnings,
              out streamIDs);
            }
            catch (Exception e)
            {
                throw new ReportException("Web Exception for Render Report", e);
            }


            if (warnings != null)
            {
                throw new ReportException("Warnings for Render Report", warnings);
            }

            return result;
        }

        /// <summary>
        /// Renders Report
        /// </summary>
        /// <param name="reportName">The report name</param>
        /// <param name="parameterList">The parameters of the report</param>
        /// <param name="outputType">The format of the report</param>
        /// <param name="dataSource">The DataSource of the report</param>
        /// <returns></returns>
        public byte[] RenderReport(ReportName reportName, ParameterValue[] parameterList, OutputType outputType, DataSource[] dataSource)
        {
            if (reportName == null)
                throw new ArgumentException("Report Name is null", "reportName");

            if (string.IsNullOrEmpty(ReportServer))
                throw new ArgumentException("ReportServer is null", "ReportServer");


            if (parameterList == null)
                throw new ArgumentException("parameterList is null", "parameterList");


            Setup();

            IsAlive();


            //create temp report
            string reportDefiniation = ReportingServiceHelper.DownloadReport(mService, reportName);

            //create temp report
            ReportName tempReportName = new ReportName(DateTime.Now.ToString("dd_MM_yyy_hh_mm_ss") + reportName.Name, reportName.Folder); // eg CRR/test

            //upload new report

            ReportingServiceHelper.UploadReport(mService, tempReportName, reportDefiniation);

            byte[] result = null;

            try
            {

                DataSource[] currentDatasource = mService.GetReportDataSources(reportName.ToString());

                if (dataSource != null)
                {
                    if (dataSource.Length > 0)
                    {
                        mService.SetReportDataSources(tempReportName.ToString(), currentDatasource);
                    }
                }


                string devInfo = @"<DeviceInfo><Toolbar>False</Toolbar></DeviceInfo>";
                string encoding;
                string mimeType;
                Warning[] warnings = null;
                ParameterValue[] reportHistoryParameters = null;
                string[] streamIDs = null;
                ParameterValue[] reportParameters = parameterList;


                result = mService.Render(reportName.ToString(), outputType.ToString(), null, devInfo, reportParameters, null,
              null, out encoding, out mimeType, out reportHistoryParameters, out warnings,
              out streamIDs);

                if (warnings != null)
                {
                    throw new ReportException("Warnings for Render Report", warnings);
                }



            }
            catch (ReportException)
            {
                throw;
            }
            catch (Exception e)
            {
                throw new ReportException("Web Exception for Render Report", e);
            }
            finally
            {
                ReportingServiceHelper.DeleteReport(mService, tempReportName);
            }

            return result;
        }
    }
}
